package com.zdxlz.fcmp.upms.api.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zdxlz.fcmp.upms.api.entity.SystemMenu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 菜单信息表(SystemMenu)表数据库访问层
 *
 * @author aman
 * @since 2025-01-15 16:59:00
 */
@Mapper
public interface SystemMenuMapper extends BaseMapper<SystemMenu> {

    /**
     * 根据用户ID查询权限
     *
     * @param userId
     * @return
     */
    @Select("<script> " +
            "SELECT distinct sm.permission " +
            "FROM system_menu sm " +
            "     left join system_role_menu srm on sm.id = srm.menu_id and sm.status = true " +
            "     left join system_role sr on srm.role_id = sr.id " +
            "     left join system_user_role sur on sr.id = sur.role_id and sr.status = true " +
            "where 1 = 1 " +
            " <if test='userId != null'>" +
            "     and sur.user_id = #{userId}" +
            " </if>" +
            " </script>")
    List<String> getPermissionByUserId(@Param("userId") Integer userId);


    /**
     * 根据用户ID查询权限
     *
     * @param status
     * @param ids
     * @return
     */
    @Update("<script> " +
            " UPDATE system_menu SET status = #{status}" +
            " where id in (" +
            " <foreach collection='ids' item='id' separator=','>" +
            " #{id}" +
            " </foreach>" +
            ")" +
            " </script>")
    int updateMenuStatusByIds(@Param("status") Boolean status, @Param("ids") List<Integer> ids);
}
